﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.Web;
using System.Text.RegularExpressions;

/// <summary>
/// Summary description for CreateFile
/// </summary>
public class CreateFile
{
    /// <summary>
    /// 保存文件内容,自动创建目录
    /// </summary>
    /// <param name="tempDir">格式:a/b.htm,相对根目录</param>
    /// <returns></returns>
    public void SaveFile(string TxtStr, string tempDir)
    {
        try
        {
            CreateDir(GetFolderPath(true, tempDir));
            System.IO.StreamWriter sw = new System.IO.StreamWriter(System.Web.HttpContext.Current.Server.MapPath(tempDir), false, System.Text.Encoding.Unicode);
            sw.Write(TxtStr);
            sw.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    /// <summary>
    /// 创建目录
    /// </summary>
    /// <param name="dir">此地路径相对站点而言</param>
    public void CreateDir(string dir)
    {
        if (dir.Length == 0) return;
        string path = System.Web.HttpContext.Current.Server.MapPath(dir);
        if (!System.IO.Directory.Exists(path))
            System.IO.Directory.CreateDirectory(path);
    }
    /// <summary>
    /// 判断目录是否存在
    /// </summary>
    /// <param name="dir">此地路径相对站点而言</param>
    public bool ExistsDir(string dir)
    {
        if (System.IO.Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(dir)))
        {
            return true;
        }
        else
        {
            return false;
        }

    }
    /// <summary>
    /// 获得文件的目录路径
    /// </summary>
    /// <param name="isUrl">是否是网址</param>
    /// <param name="filePath">文件路径</param>
    /// <returns>以\或/结尾</returns>
    public string GetFolderPath(bool isUrl, string filePath)
    {
        if (isUrl)
            return filePath.Substring(0, filePath.LastIndexOf("/") + 1);
        else
            return filePath.Substring(0, filePath.LastIndexOf("\\") + 1);
    }
    /// <summary>
    /// 判断文件是否存在
    /// </summary>
    /// <param name="file">格式:a/b.htm,相对根目录</param>
    /// <returns></returns>
    public bool FileExists(string file)
    {
        if (File.Exists(System.Web.HttpContext.Current.Server.MapPath(file)))

            return true;
        else
            return false;
    }
    /// <summary>   
    /// 用递归方法删除文件夹目录及文件   
    /// </summary>   
    /// <param name="dir">带文件夹名的路径</param>    

    public void DeleteFolder(string dir)
    {
        try
        {
            foreach (string d in Directory.GetFileSystemEntries(dir))
            {
                if (File.Exists(d))
                {
                    FileInfo fi = new FileInfo(d);
                    if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                        fi.Attributes = FileAttributes.Normal;
                    File.Delete(d);//直接删除其中的文件   
                }
                else
                    DeleteFolder(d);//递归删除子文件夹   
            }
            Directory.Delete(dir);//删除已空文件夹   
        }
        catch (Exception s)
        {

        }
    }
    /// <summary>
    /// 删除文件
    /// </summary>
    /// <param name="dir"></param>
    public void DeleteDocument(string dir)
    {
        if (File.Exists(System.Web.HttpContext.Current.Server.MapPath(dir)))
        {
            FileInfo fi = new FileInfo(System.Web.HttpContext.Current.Server.MapPath(dir));
            if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                fi.Attributes = FileAttributes.Normal;
            File.Delete(System.Web.HttpContext.Current.Server.MapPath(dir));//直接删除其中的文件   
        }
    }
    /// <summary>
    ///  截取字符串
    /// </summary>
    /// <param name="s">字符串</param>
    /// <param name="l">长度</param>
    /// <returns></returns>
    public string subStr(string s, int l)
    {
        if (l == 0 || s == null)
        {
            return s;
        }
        else
        {
            string endStr = "...";
            string temp = s.Substring(0, (s.Length < l + 1) ? s.Length : l + 1);
            byte[] encodedBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(temp);

            string outputStr = "";
            int count = 0;

            for (int i = 0; i < temp.Length; i++)
            {
                if ((int)encodedBytes[i] == 63)
                    count += 2;
                else
                    count += 1;

                if (count <= l - endStr.Length)
                    outputStr += temp.Substring(i, 1);
                else if (count > l)
                    break;
            }

            if (count <= l)
            {
                outputStr = temp;
                endStr = "";
            }

            outputStr += endStr;

            return outputStr;
        }
    }
    /// <summary>
    /// 生成随机码
    /// </summary>
    /// <param name="VcodeNum"></param>
    /// <returns></returns>
    public string RndNum(int VcodeNum)
    {
        string Vchar = "1,2,3,4,5,6,7,8,9,0,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,i,s,t,u,v,w,x,y,z";
        string[] VcArray = Vchar.Split(',');
        string VNum = "";//由于字符串很短，就不用StringBuilder了  
        int temp = -1;//记录上次随机数值，尽量避免生产几个一样的随机数  

        //采用一个简单的算法以保证生成随机数的不同  
        Random rand = new Random();
        for (int i = 1; i < VcodeNum + 1; i++)
        {
            if (temp != -1)
            {
                rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
            }
            int t = rand.Next(37);
            // int t = rand.Next(10);
            if (temp != -1 && temp == t)
            {
                return RndNum(VcodeNum);
            }
            temp = t;
            VNum += VcArray[t];

        }
        return VNum;
    }



    private int DirectoryName(string DirectoryPath)//获取文件夹名，截取“\” 
    {

        int j = 0; char[] c = DirectoryPath.ToCharArray();

        for (int i = c.Length - 1; i >= 0; i--)//从后面截取    
        {

            j = i;

            if (c[i] == '\\')
            {

                break;//遇"\"调处,并返回"\"的位置        

            }

        }

        return j + 1;

    }
    /// <summary>
    ///拷贝文件夹及其子文件和内容
    /// </summary>
    /// <param name="DirectoryPath"></param>
    /// <param name="DirAddress"></param>
    public void CopyDirectory(string DirectoryPath, string DirAddress)//复制文件夹，
    {

        #region//递归

        string s = DirectoryPath.Substring(DirectoryName(DirectoryPath));//获取文件夹名    

        if (Directory.Exists(DirAddress + "\\" + s))
        {

            Directory.Delete(DirAddress + "\\" + s, true);//若文件夹存在，不管目录是否为空，删除 

            Directory.CreateDirectory(DirAddress + "\\" + s);//删除后，重新创建文件夹     

        }

        else
        {

            Directory.CreateDirectory(DirAddress + "\\" + s);//文件夹不存在，创建     

        }

        DirectoryInfo DirectoryArray = new DirectoryInfo(DirectoryPath);

        FileInfo[] Files = DirectoryArray.GetFiles();//获取该文件夹下的文件列表     

        DirectoryInfo[] Directorys = DirectoryArray.GetDirectories();//获取该文件夹下的文件夹列表     

        foreach (FileInfo inf in Files)//逐个复制文件     
        {

            File.Copy(DirectoryPath + "\\" + inf.Name, DirAddress + "\\" + s + "\\" + inf.Name,true);

        }

        foreach (DirectoryInfo Dir in Directorys)//逐个获取文件夹名称，并递归调用方法本身     
        {
            if (Dir.Name != ".svn" && Dir.Name != "_svn")
            {
                CopyDirectory(DirectoryPath + "\\" + Dir.Name, DirAddress + "\\" + s);
            }

        }

        #endregion

    }
    /// <summary>
    /// js输出内容时替换异常字符
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public string JSSReplace(string str)
    {
        System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"<!--(.*?)-->", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        str = regex.Replace(str, "");
        return str.Replace("\"", "\\\"");
    }

    public string ReturnAction(string Action)
    {
        var AName = "";
        switch (Action)
        {
            case "Apply": AName = "提交申请"; break;
            case "InCheck": AName = "审核"; break;
            case "OutCheck": AName = "审核"; break;
            case "OutSideCheck": AName = "外转审批"; break;
            case "Receive": AName = "接收"; break;
            case "Receipt": AName = "已回执"; break;
            case "Receipted": AName = "已回执"; break;
            case "Back": AName = "回退"; break;
            case "Edit": AName = "修改"; break;
            case "Abate": AName = "废件"; break;
                
        }
        return AName;
    }



}